package com.harmonyos.studyharmonyos;

import com.harmonyos.studyharmonyos.slice.CommonCompontsAbilitySlice;
import ohos.aafwk.ability.*;
import ohos.aafwk.content.Intent;
import ohos.utils.zson.ZSONObject;

import java.util.Map;
import java.util.Random;

/**
 * 新增了服务卡片功能
 *
 * @author lianghao
 * @version V1.0
 * @desc: CommonCompontsAbility.java(用一句话描述该文件做什么)
 * @date 2021/7/8 15:23
 */
public class CommonCompontsAbility extends Ability {
    private static final String EMPTY_STRING = "";
    private static final int INVALID_FORM_ID = -1;
    private static final int DEFAULT_DIMENSION_2X2 = 2;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(CommonCompontsAbilitySlice.class.getName());
//        setAbilitySliceAnimator(null);
    }

    /* 卡片功能--------------------start*/
    @Override
    protected ProviderFormInfo onCreateForm(Intent intent) {
        ProviderFormInfo providerFormInfo = new ProviderFormInfo();
        if (intent == null) {
            return providerFormInfo;
        }
        // 获取卡片id
        long formId = INVALID_FORM_ID;
        if (intent.hasParameter(AbilitySlice.PARAM_FORM_IDENTITY_KEY)) {
            formId = intent.getLongParam(AbilitySlice.PARAM_FORM_IDENTITY_KEY, INVALID_FORM_ID);
        }
        // 获取卡片名称
        String formName = EMPTY_STRING;
        if (intent.hasParameter(AbilitySlice.PARAM_FORM_NAME_KEY)) {
            formName = intent.getStringParam(AbilitySlice.PARAM_FORM_NAME_KEY);
        }
        // 获取卡片规格
        int dimension = DEFAULT_DIMENSION_2X2;
        if (intent.hasParameter(AbilitySlice.PARAM_FORM_DIMENSION_KEY)) {
            dimension = intent.getIntParam(AbilitySlice.PARAM_FORM_DIMENSION_KEY, DEFAULT_DIMENSION_2X2);
        }
        FormBindingData formBindingData = new FormBindingData("{}");
        providerFormInfo.setJsBindingData(formBindingData);
        return providerFormInfo;
    }

    @Override
    protected void onDeleteForm(long formId) {
        super.onDeleteForm(formId);
    }

    /**
     * 更新卡片数据
     *
     * @param formId 卡片ID
     */
    @Override
    protected void onUpdateForm(long formId) {
        super.onUpdateForm(formId);
        ZSONObject zsonObject = new ZSONObject();
        zsonObject.put("", "");
        FormBindingData formBindingData = new FormBindingData(zsonObject);
        // 调用updateForm接口去更新对应的卡片，仅更新入参中携带的数据信息，其他信息保持不变
        try {
            updateForm(formId, formBindingData);
        } catch (FormException e) {
            e.fillInStackTrace();
        }
    }

    @Override
    protected void onEventNotify(Map<Long, Integer> formEvents) {
        super.onEventNotify(formEvents);
    }

    @Override
    protected void onTriggerFormEvent(long formId, String message) {
        onUpdateForm(formId);
        super.onTriggerFormEvent(formId, message);
    }

    @Override
    protected void onCastTempForm(long formId) {
        super.onCastTempForm(formId);
    }
    /* 卡片功能--------------------end*/
}

